<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\system\SetApp;
use app\models\system\SetBasic;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserBank;
use app\models\user\UserCash;

class CashController extends H5Controller
{
    public function actionList()
    {
        $status = -100;
        $type = intval($this->post('type'));

        if ($type == 1) {
            $status = 1;
        }
        if ($type == 2) {
            $status = 0;
        }
        if ($type == 3) {
            $status = -1;
        }

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCash::find()
            ->andWhere(['user_id' => $this->user->id]);

        if ($status > -100) {
            $query->andWhere(['status' => $status]);
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionFetch()
    {
        $set = SetBasic::getLastOne();
        $mini = $set->cash_min_money;
        $day = $set->cash_total_for_day;
        $service = $set->cash_service;

        $user = User::findOne(['id' => $this->user->id]);
        $account = UserAccount::findOne(['user_id' => $this->user->id]);

        $bank = UserBank::find()
            ->where(['user_id' => $this->user->id])
            ->asArray()
            ->one();

        return $this->successEncode([
            'set' => [
                'mini' => $mini,
                'day' => $day,
                'service' => $service,
            ],
            'user' => [
                'id' => $user->id,
                'nickname' => $user->nickname,
                'mobile' => $user->mobile,
            ],
            'balance' => $account->balance,
            'bank' => $bank
        ]);
    }

    public function actionSave()
    {
        // return $this->fail('操作失败,系统升级中,请耐心等待~');

        $set_app = SetApp::getLastOne();
        // var_dump($set_app->open_test);exit;
        if ($set_app->open_test == 1) {
            return $this->fail('操作失败,系统升级中,请耐心等待~');
        }

        $data = $this->post();

        $set = SetBasic::getLastOne();
        $account = UserAccount::findOne(['user_id' => $this->user->id]);

        // mini
        if ($data['amount'] < $set->cash_min_money) {
            return $this->fail('提现金额错误');
        }

        // cash for day
        $count = UserCash::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['DATE_FORMAT(create_time, "%m-%d")' => date('m-d')])
            ->count();

        // var_dump($count);exit;

        if ($count + 1 > $set->cash_total_for_day) {
            return $this->fail('提现次数超过,每天只能提现:' . intval($set->cash_total_for_day) . '次');
        }

        // balance
        if ($data['amount'] > $account->balance) {
            return $this->fail('余额不足');
        }

        // bank
        if (empty($data['bank']) || !$data['bank']['bank_number'] || !$data['bank']['nickname']) {
            return $this->fail('请填写银行卡信息');
        }

        $transaction = \Yii::$app->db->beginTransaction();

        try {
            // account
            $account->balance -= $data['amount'];
            $account->save();

            // cash record
            $model = new UserCash();
            $model->sn = UserCash::generateSn();
            $model->amount = $data['amount'];
            $model->service = ($set->cash_service / 100) * $data['amount'];
            $model->user_id = $this->user->id;
            $model->status = 0;
            $model->nickname = $data['bank']['nickname'];
            $model->bank_number = $data['bank']['bank_number'];
            $model->bank_name = $data['bank']['bank_name'];
            $model->mobile = $data['bank']['mobile'];
            $model->bank_x = $data['bank']['bank_x'];
            $model->save();

            $transaction->commit();
            return $this->success();
        } catch (\Exception $e) {
            $transaction->rollBack();
            return $this->fail($e->getMessage());
        }
    }


}